package palindrome;

/**
 * @author Jarvan
 * @version 1.0
 * @create 2020/10/13 9:49
 */
public class Palindrome {
    /**
     * Return true if the string is palindrome.
     * Use recursion
     *
     * @param chars           string to char[] .
     * @param start           start index of the string.
     * @param end             end index of the string.
     * @return return true if the string is palindrome.
     */
    public static boolean isPalindrome(char[] chars, int start, int end) {
        if (chars.length % 2 == 0) {
            //if the string length is double (even number ),it can not be palindrome.
            return false;
        }

        if (end-start+1 == 1) {
            //if the string length is 1 ,it must be a palindrome.
            return true;
        }

        //compared with the start and end.
        if (chars[start] != chars[end] || start >= end) {
            return false;
        }
        return isPalindrome(chars,start + 1,end-1);
    }
}
